package com.jackup.jasp.internet;


import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jackup.jasp.bean.Login_bean;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import base.Constant;

/**
 * Created by wangic on 2017/2/6.
 */
public class Login_utils {
    private static final String TAG = "Login_utils";


    public static void Login(String user_name, String user_pwd, final Handler handler){
        RequestParams login_params = new RequestParams(Constant.login_url);
        Login_bean login_bean = new Login_bean();
        login_bean.setAccount(user_name);
        login_bean.setPassword(user_pwd);
        login_params.addBodyParameter("account",login_bean.getAccount());
        login_params.addBodyParameter("password",login_bean.getPassword());
        Log.e(TAG, "Login: "+login_bean.getAccount()+user_pwd );
        x.http().post(login_params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                Log.e(TAG, "onSuccess: "+result );
                try {
                    Message msg = new Message();
                    if((boolean)result.get("data")==true){
                        msg.what = 0;
                        handler.sendMessage(msg);
                    }

                } catch (JSONException e) {
                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Message msg = new Message();
                msg.what = Constant.ERROR_MSG;
                handler.sendMessage(msg);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
